package com.service.impl;

import com.dao.UserDao;
import com.entity.ResponseEntity;
import com.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private UserDao userDao;

    @Autowired(required = false)//这个注解的required的意思：在编译期间不去检查有没有这个bean对象
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public ResponseEntity findAll() {
        List<User> list = userDao.findAll();
        return new ResponseEntity(200,"查询成功",list);
    }

    @Override
    public ResponseEntity findById(int id) {
        User user = userDao.findById(id);
        return new ResponseEntity(200,"查询成功",user);
    }

    @Override
    public ResponseEntity addOne(User user) {
        int i = userDao.addOne(user);
        if(i > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.FAIL;
    }

    @Override
    public ResponseEntity updateOne(User user) {
        int i = userDao.updateOne(user);
        if(i > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.FAIL;
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int i = userDao.deleteOne(id);
        if(i > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.FAIL;
    }

    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> all = userDao.findAll();
        PageInfo<User> pageInfo = PageInfo.of(all);
        return new ResponseEntity<>(200,"查询成功",pageInfo);
    }
}
